package com.cloudptt.api.product.func.call.group.fsm.floor;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.models.call.RtcpMsg;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.log.Log;



public class FloorPermissionState implements ISubState
{
	private FloorAgent agent = null;

	public FloorPermissionState(FloorAgent agent)
	{
		this.agent = agent;
		Log.d(FloorAgent.Log_Key, "currentState is FloorPermissionState ");
		this.agent.opAudioMedia(CallAttr.OpMedia_Resume);
	}

	public int doUserAction(int btnAction)
	{
		if(CallConst.BtnAction_Release == btnAction)
		{
			int result = agent.floorRelease();
			if(CauseCont.OperateCheck_Suc == result)
			{
				agent.goToState(CallConst.FloorState_PendingRelease,null);
			}
			return result;
		}
		return CauseCont.OperateErr_Invalid;
	}
	
	public void receiveRtcpMsg(RtcpMsg msg)
	{
		Log.d(FloorAgent.Log_Key, "" + CallConst.stateTag(getState()) + " receiveRtcpMsg msg = " + msg);
		if(msg == null)
		{
			return;
		}

		String title = msg.getTitle();
		if(title.equals(RtcpMsg.Title_FLOOR_REVOKE))
		{
			int cause = msg.getCause();
			Log.d(FloorAgent.Log_Key, "" + CallConst.stateTag(getState()) + " receiveRtcpMsg cause = " + cause);
			int result =  agent.floorRelease();
			if(result == CallConst.Operate_Suc)
			{
				agent.goToState(CallConst.FloorState_PendingRelease,null);
			}
		}
		else if(title.equals(RtcpMsg.Title_FLOOR_GRANTED))
		{
		}
		if(title.equals(RtcpMsg.Title_FLOOR_IDLE))
		{
			agent.goToState(CallConst.FloorState_NoPermission,null);
		}
	}
	
	public int getState()
	{
		return CallConst.FloorState_Permission;
	}
	public void destory()
	{
//		this.agent.opAudioMedia(0);
		agent = null;
	}
}
